<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\model\BooksModel;
use app\model\RedisModel;
use app\model\UserModel;
use app\model\UserReadingRecordsModel;
use think\App;
use think\facade\Db;

class UserReadingRecords extends Base {

    private $model;

    private $bookModel;

    private $userModel;
    private $req;

    private $uid;

    private $page;

    private $cacheKey="user:";

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->uid = $this->isLogin();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->bookModel = new BooksModel();

        $this->userModel = new UserModel();

        $this->model = new UserReadingRecordsModel();
    }



    public function getUserBookReadLog(){

        if(!$this->uid){
            return $this->succ();
        }

        if(empty($this->req["book_id"])){
            return $this->succ();
        }


        $where=[];
        $where['uid'] = $this->uid;
        $where['book_id'] = $this->req["book_id"];

        $list = $this->model->getList($where,0,1,"created_at","DESC");
        if(empty($list)){
            return $this->succ();
        }

        return $this->succ($list[0]);
    }





    /*
     * 添加用户阅读记录
     * */

    public function addUserBookReadLog()
    {
        if(!$this->uid){
            return $this->fail("请先登录");
        }

        if(!isset($this->req["book_id"]) || empty($this->req["book_id"])){
            return $this->fail("书籍Id错误");
        }

        if(!isset($this->req["chapter_id"])  || empty($this->req["chapter_id"])){
            return $this->fail("章节Id错误");
        }


        $data=[];
        $data['uid'] = $this->uid;
        $data['book_id'] = $this->req["book_id"];
        $data["chapter_id"] = $this->req["chapter_id"];
        $where = $data;
        $haveRead = $this->model->getList($data,0,1);

        Db::startTrans();
        if(!empty($haveRead)){
            if($haveRead[0]["is_finished"]){
                return  $this->succ([],"章节已读完，不可重复获取奖励");
            }

            //更新操作
            $data=[];
            $data["progress"] = $this->req["progress"];
            $data["is_finished"] = $this->req["is_finished"];
            $data["updated_at"] = date("Y-m-d H:i:s");
            if($data["is_finished"]){
                $data["fb"] =  $this->bookModel->getChapterFb($this->req["book_id"]);
                $res2 = $this->userModel->incrUserFeild($this->uid,"fb", $data["fb"]);
            }else{
                $data["fb"] = 0;
                $res2 = true;
            }
            $res1 = $this->model->updateOne($where,$data);
        }else{
            $data["progress"] = $this->req["progress"];
            $data["is_finished"] = $this->req["is_finished"];
            if($data["is_finished"]){
                $data["fb"] =  $this->bookModel->getChapterFb($this->req["book_id"]);
                $res2 = $this->userModel->incrUserFeild($this->uid, "fb",$data["fb"]);
            }else{
                $data["fb"] = 0;
                $res2 = true;
            }
            $res1 = $this->model->addOne($data);
        }

        if($res1 && $res2){
            Db::commit();
            $this->delCache($this->uid);
            return  $this->succ([],"成功");
        }

        Db::callback();
        return  $this->fail("失败");
    }




    public function delCache($uid)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$uid);
    }

}
